package com.yanlin.servicemap.service;

import com.yanlin.internalcommon.dto.ResponseResult;
import com.yanlin.internalcommon.request.DicDistrict;
import com.yanlin.servicemap.client.DicDistrictClient;
import com.yanlin.servicemap.mapper.DicDistrictMapper;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.yanlin.internalcommon.constant.AmapConstant.*;
import static com.yanlin.internalcommon.constant.CommonStatusEnum.AMAP_REQUEST_EROOR;

/**
 * <p>标题：DistricService</p>
 * <p>功能：</p>
 * <p>
 * 其他说明：
 * </p>
 * <p>审核：</p>
 * <p>重构：</p>
 * <p>创建日期：2025-06-11 11:28</p>
 *
 * @author yanlin
 */
@Service
@Slf4j
public class DistrictService {


    @Autowired
    private DicDistrictClient dicDistrictClient;
    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    public ResponseResult initDistrict(String keywords) {
        // 获取map 数据
        ResponseResult<String> districtData = dicDistrictClient.getDistrictData(keywords);
        String data = districtData.getData();
        JSONObject jsonDistrict = null;
        try {
            jsonDistrict = JSONObject.fromObject(data);
            // 判断状态1，0
            int status = jsonDistrict.getInt(AMAP_STATUS);
            if (status != 1) {
                log.warn("dic districts parse error ");
                return ResponseResult.fail(AMAP_REQUEST_EROOR.getCode(), AMAP_REQUEST_EROOR.getValue());
            }
            JSONArray countryArray = jsonDistrict.getJSONArray(AMAP_DISTRICT);
            for (int country = 0; country < countryArray.size(); country++) {
                JSONObject countryObject = countryArray.getJSONObject(country);
                String countryAdCode = countryObject.getString(AMAP_ADDRESS_CODE);
                JSONArray provinceArray = countryObject.getJSONArray(AMAP_DISTRICT);
                insertDicDistrict(countryObject, "0");
                for (int province = 0; province < provinceArray.size(); province++) {
                    JSONObject provinceObject = provinceArray.getJSONObject(province);
                    String provinceAdCode = provinceObject.getString(AMAP_ADDRESS_CODE);
                    JSONArray cityArray = provinceObject.getJSONArray(AMAP_DISTRICT);
                    insertDicDistrict(provinceObject, countryAdCode);
                    for (int city = 0; city < cityArray.size(); city++) {
                        JSONObject cityObject = cityArray.getJSONObject(city);
                        String cityAdCode = cityObject.getString(AMAP_ADDRESS_CODE);
                        JSONArray districtArray = cityObject.getJSONArray(AMAP_DISTRICT);



                        insertDicDistrict(cityObject, provinceAdCode);
                        for (int district = 0; district < districtArray.size(); district++) {
                            JSONObject districtObject = districtArray.getJSONObject(district);
                            insertDicDistrict(districtObject, cityAdCode);
                        }
                    }
                }

            }

        } catch (Exception e) {
            log.warn("amap json parse error");
            e.printStackTrace();
        }

        return ResponseResult.success(districtData.getData());
    }


    private void insertDicDistrict(JSONObject object, String parentAdCode) {
        // 数据库对象
        DicDistrict dicDistrict = new DicDistrict();
        String level = object.getString(AMAP_LEVEL);
        switch (level) {
            case "country":
                dicDistrict.setLevel(1);
                break;
            case "province":
                dicDistrict.setLevel(2);
                break;
            case "city":
                dicDistrict.setLevel(3);
                break;
            case "district":
                dicDistrict.setLevel(4);
                break;
            case "street":
                return;
        }
        dicDistrict.setAddressCode(object.getString(AMAP_ADDRESS_CODE));
        dicDistrict.setAddressName(object.getString(AMAP_ADDRESS_NAME));
        dicDistrict.setParentAddressCode(parentAdCode);
        // 插入数据库
        dicDistrictMapper.insert(dicDistrict);
    }
}